package com.leapp.sk.viewdelegate.join

import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.ListView
import android.widget.PopupWindow
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.PopupWindowUtil
import com.example.mvp.view.SKTopBar
import com.leapp.sk.R
import com.leapp.sk.bean.ChoseClassEntity
import com.leapp.sk.bean.TopLayoutTitleEntity
import com.leapp.sk.ui.activity.album.EditAlbumActivity
import com.leapp.sk.ui.activity.task.CompleteTaskActivity
import com.leapp.sk.ui.adapter.TopChoseClassAdapter
import java.lang.reflect.Field

/**
 * 入班审核视图托管类
 * @author  chaohaipeng  on 2018/8/14.
 */
class JoinClassExaminationDelegate : BaseAppDelegate(){
    /**
     * fragment数据集合
     */
    private  var fragmentLists:List<Fragment> ?=null
    /**
     * 标签集合
     */
    private var titles:List<TopLayoutTitleEntity>?=null
    private var popupWindow: PopupWindow? = null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_jion_class_examination
    }

    private fun initFootView(context: Context,mlist: List<ChoseClassEntity>): View {
        val view  = LayoutInflater.from(context).inflate(R.layout.layout_join_class_chose_popuwindow, null)
        val findViewById = view.findViewById<ListView>(R.id.lv_ljccp_list)
        val topChoseClassAdapter = TopChoseClassAdapter(context, mlist)
        findViewById.adapter=topChoseClassAdapter
        findViewById.setOnItemClickListener{adapterView, view, i, l ->
            popupWindow?.dismiss()

        }


        return view
    }

    /**
     * 初始化班级
     */
  fun  initClassData(context: Context,mlist: List<ChoseClassEntity>){
      popupWindow = PopupWindowUtil.getInstance().initView(initFootView(context,mlist), LKScreenUtil.getScreenWidth(), LinearLayout.LayoutParams.WRAP_CONTENT)
  }


    /**
     * 设置标题
     */
    fun setTitle(title:String?){
        val topBar= getView<SKTopBar>(R.id.jtb_ajce_examination)
        topBar.inVisibleLine()
        if (!TextUtils.isEmpty(title)){
            topBar?.setTitle(title)
        }
        setEvent(topBar)
    }
    /**
     * 设置设置事件
     */
    private  fun setEvent(topBar: SKTopBar){
        //消失监听听
        popupWindow?.setOnDismissListener(object:PopupWindow.OnDismissListener{
            override fun onDismiss() {
                topBar.setImageSelector(false)
            }

        })
        topBar.inVisibleBottom()
        topBar?.setEventBottomInterface(object :SKTopBar.EventBottomInterface{
            override fun openOnClick() {
                val showing = popupWindow?.isShowing
               if (showing!!){
                   popupWindow?.dismiss()
               }else{
                   topBar.setImageSelector(true)
                   popupWindow?.showAsDropDown(topBar,0,0)
               }
            }

        })

        topBar?.setEventInterface(object : SKTopBar.EventInterface{
            override fun leftOnClick() {
                getActivity<AppCompatActivity>().finish()
            }
            override fun rightOnClick() {
                val intent= Intent(getActivity<AppCompatActivity>(), CompleteTaskActivity::class.java)
                getActivity<AppCompatActivity>().startActivity(intent)
            }

        })
    }
    /**
     * 初始化Tablayout
     */
    fun initTabLayout(activity: AppCompatActivity, fragmentLists:List<Fragment>, titles:List<TopLayoutTitleEntity>){
        if (fragmentLists.size==titles.size){
            this.fragmentLists= fragmentLists
            this.titles=titles
            val tab_atl_leave_toblayout = getView<TabLayout>(R.id.tab_ajce_leave_toblayout)
            val vp_atl_leave_container = getView<ViewPager>(R.id.vp_ajce_leave_container)
            val mFragmentAdapteradapter = FragmentAdapter(activity.supportFragmentManager, fragmentLists)
            vp_atl_leave_container?.adapter=mFragmentAdapteradapter
            tab_atl_leave_toblayout?.post(Runnable { setIndicator(tab_atl_leave_toblayout, 20, 20) })
            //将TabLayout和ViewPager关联起来。
            tab_atl_leave_toblayout?.setupWithViewPager(vp_atl_leave_container)
        }

    }

    /**
     * FragmentAdapter
     */
    private inner class FragmentAdapter(fm: FragmentManager, private val mFragments: List<Fragment>) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return mFragments[position]
        }

        override fun getCount(): Int {
            return mFragments.size
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles?.get(position)?.title.toString()
        }
    }

    /**
     * 选项卡设置
     */
    private  fun setIndicator(tabs: TabLayout, leftDip: Int, rightDip: Int) {
        val tabLayout = tabs.javaClass
        var tabStrip: Field? = null
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip")
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        }

        tabStrip!!.isAccessible = true
        var llTab: LinearLayout? = null
        try {
            llTab = tabStrip.get(tabs) as LinearLayout
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }

        val left = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip.toFloat(), Resources.getSystem().displayMetrics).toInt()
        val right = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip.toFloat(), Resources.getSystem().displayMetrics).toInt()

        for (i in 0 until llTab!!.childCount) {
            val child = llTab.getChildAt(i)
            child.setPadding(0, 0, 0, 0)
            val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1f)
            params.leftMargin = left
            params.rightMargin = right
            child.layoutParams = params
            child.invalidate()
        }

    }
}